<?php

include_once $_SERVER['DOCUMENT_ROOT'].'HelmesWorkTest/backend/model/data/Sector.php';
include_once $_SERVER['DOCUMENT_ROOT'].'HelmesWorkTest/backend/model/dao/SectorDAO.php';
include_once $_SERVER['DOCUMENT_ROOT'].'HelmesWorkTest/GeneralConstants.php';
/**
 * Description of SectorDAOMySQLImpl
 *
 * @author Reinowin7
 */
define ("WARNING_NO_ROWS_AFFECTED", 0);

class SectorDAOMySQLImpl implements SectorDAO
{    
    private $dbConnection;
    private $rowsAffected;

    public function __construct($dbConnection)
    {
        $this->dbConnection = $dbConnection;
    }
    
    public function getAllSectors()
    {
        $listOfAllSectors = array();
        
        $sql = 'SELECT * FROM Sectors';
        
        $resultSet = mysql_query($sql, $this->dbConnection);
        
        if (!$resultSet)
        {
            ConnectionManager::getInstance()->closeResultSet($resultSet);
            return null;
        }
        
        while ($row = mysql_fetch_array($resultSet, MYSQL_ASSOC)) 
        {
             $listOfAllSectors[] = $this->fillSectorWithDataFromDatabase($row);
        }
        
        ConnectionManager::getInstance()->closeResultSet($resultSet);
        return $listOfAllSectors;
    }
    
    //----------HELPER METHODS----------------
    private function fillSectorWithDataFromDatabase($databaseRow)
    {
        $sectorRetrieved = null;
        
        try 
        {
            $sectorRetrieved = new Sector();
            $sectorRetrieved->setSectorID($databaseRow["sector_id"]);
            $sectorRetrieved->setSectorName($databaseRow["sector_name"]);
            $sectorRetrieved->setParentGeneralSector($databaseRow["parent_general_sector_id"]);
            $sectorRetrieved->setLevelInHierarchy($databaseRow["level_in_hierarchy"]);
        }
        catch (Exception $e) 
        {
            $e->printStackTrace();
        }

        return $sectorRetrieved;
    }
}

?>
